package com.xuecheng.test.demo.video;

import java.awt.image.BufferedImage;

/**
 * 将img图转化为字符画
 */
public class ImageToStr {

    public static BufferedImage createAsciiPic(BufferedImage image) {
        //final String base = "@#&$%*o!;.";// 字符串由复杂到简单
        int[] rgbList= {-16777216,-16050362,-8353145,-6056832,-2302756,-4144960,-657931,-1973791};
        //final BufferedImage image = ImageIO.read(new File(path));
        for (int y = 0; y < image.getHeight(); y ++) {
            for (int x = 0; x < image.getWidth(); x++) {
                final int pixel = image.getRGB(x, y);
                //image.setRGB(x,y,);
                //算出R G B三个值
                final int r = (pixel & 0xff0000) >> 16, g = (pixel & 0xff00) >> 8, b = pixel & 0xff;
                //赋予权重
                float gray = 0.299f * r + 0.578f * g + 0.114f * b;
                //计算出最最终结果
                int index = Math.round(gray * (rgbList.length) / 255);
                if(index >=8){
                    index = index-1;

                }
                if(index == 1){
                    index = 0;
                }
                image.setRGB(x,y,rgbList[index]);
            }

        }
        return image;
    }

    public static void main(String args []){
        ImageToStr.RGBtoInteger();
        //ImageToStr.createAsciiPic("C:\\Users\\admin\\Desktop\\1583466422(1).jpg");
    }

    /**
     * 根据rgb值算出程序认可的int数值
     */
    public static void RGBtoInteger(){
        int r = 11;
        int g = 23;
        int b = 70;
        int pixel = ((0xFF << 24)|(r << 16)|(g << 8)|b);
        System.out.println(" : R="+r+"，G="+g+"，B="+b+" RGB对应的int值："+pixel);
    }
    //int[] rgbList= {-16777216,-16050362,-8353145,-6056832,-2302756,-4144960,-657931,-1973791};
    /**
     * 黑色 ：R=0，G=0，B=0 RGB对应的int值：-16777216
     * jackie blue : R=11，G=23，B=70 RGB对应的int值：-16050362
     * 冷灰 : R=128，G=138，B=135 RGB对应的int值：-8353145
     * 米色 : R=163，G=148，B=128 RGB对应的int值：-6056832
     * gainsboro : R=220，G=220，B=220 RGB对应的int值：-2302756
     * 灰色	：R=192，G=192，B=192 RGB对应的int值：-4144960
     * 白烟 : R=245，G=245，B=245 RGB对应的int值：-657931
     * 白色 ：R=225，G=225，B=149 RGB对应的int值：-1973791
     */
}